package inglar.autotc.client;

import inglar.autotc.client.resource.ResourceHandler;
import inglar.autotc.client.ui.PageActions;
import inglar.autotc.client.ui.PageClients;
import inglar.autotc.client.ui.PageDevices;
import inglar.autotc.client.ui.PageWelcome;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class AutoTC implements EntryPoint {
	RootPanel contentPanel = null;
	private TabPanel tabPanel;
	private AsyncCallback<String> checkInstallationCallback;
	private HorizontalPanel panelLoading;
	private Image imgLoading;
	private Label lblLoading;
	
	public void onModuleLoad() {
		contentPanel = RootPanel.get("content");
		
		if(!Cookies.isCookieEnabled()) {
			contentPanel = RootPanel.get("content");
			HTML _html = new HTML(ResourceHandler.INSTANCE.noCookies().getText());
			contentPanel.add(_html);
			return;
		}
		
		/* section LOADING: this get shown while the webapp is fetching information */
		panelLoading = new HorizontalPanel();
		
		lblLoading = new Label("Loading Qontrol-II.WEBAPP...");
		lblLoading.getElement().setAttribute("style", "margin-right: 10px; font-weight: bold;");
		panelLoading.add(lblLoading);
		
		imgLoading = new Image(ResourceHandler.INSTANCE.fbLoadBlue());
		panelLoading.add(imgLoading);
		
		contentPanel.add(panelLoading);
		/* end LOADING */
		
		checkInstallationCallback = new AsyncCallback<String>() {
			@Override
			public void onSuccess(String result) {
//				Window.alert(result);
				showTabs(result);
			}
			@Override
			public void onFailure(Throwable caught) {
				Utilities.failureAlert("Base.checkInstallationCallback", "RPC failure", caught);
			}
		};
		
		Utilities.rpc.checkInstallation(checkInstallationCallback);
	}
	
	private void showTabs(String message) {
		message = message.trim();
		
		tabPanel = new TabPanel();
		contentPanel.clear();
		contentPanel.add(tabPanel, 0, 0);
		tabPanel.setAnimationEnabled(false);
		tabPanel.getElement().setId("tab-main");
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				Cookies.setCookie("QP", event.getSelectedItem().toString());
			}
		});
		
		PageWelcome user = new PageWelcome(message);
		tabPanel.add(user, "Welcome", false);
		user.getElement().setId("page-welcome");
		
		if(Utilities.isLogged() && message.isEmpty()) {
			PageDevices tcs = new PageDevices();
			tabPanel.add(tcs, "QDiscs and Classes", false);
			tcs.getElement().setId("page-devices");
			
			PageClients clients = new PageClients();
			tabPanel.add(clients, "Clients", false);
			tcs.getElement().setId("page-clients");
			
			PageActions actions = new PageActions();
			tabPanel.add(actions, "Actions", false);
			tcs.getElement().setId("page-actions");
			
			// XXX add more tabs here
			
		}
		
		// TODO add manual page, must be viewable even when not logged in

		tabPanel.setAnimationEnabled(true);
		String tabselects = Cookies.getCookie("QP");
		int tabselect = 0;
		try {
			tabselect = Integer.parseInt(tabselects);
		} finally {
			tabPanel.selectTab(tabselect);
		} 
	}
}
